/**
* Map.cpp
*/

#include "Map.h"

Map::Map() {
		this->graph = new Graph();
		this->mapObjectsList = new List();
	}

Map::~Map() {
	delete mapObjectsList;
}

Graph* Map::getGraph() const
{
	return graph;
}

void Map::setGraph(Graph* graph)
{
	this->graph = graph;
}

List* Map::getMapObjectsList() const
{
	return mapObjectsList;
}

void Map::setMapObjectsList(List* mapObjectsList)
{
	this->mapObjectsList = mapObjectsList;
}

 int Map::createMapObject(string id, string idEdge, int number, string type)
 {
	 //Cheks for duplicate ID's
	 if (getMapObject(id) != NULL) return ID_REPEATED;
	 else {
		//Validates edge and number in range
		 if ((getGraph()->getEdge(idEdge) == NULL) || (number < Utils::min(getGraph()->getEdge(idEdge)->getInitAddress(),getGraph()->getEdge(idEdge)->getLastAddress())) || (number > Utils::max(getGraph()->getEdge(idEdge)->getInitAddress(),getGraph()->getEdge(idEdge)->getLastAddress(),0,0))) return INVALID_EDGE;
		else {
			MapObject* mapObject = new MapObject(id,idEdge,number,type);
			MapObjectNode* node = new MapObjectNode();
			if ((mapObject ==NULL) || (node == NULL)) return MEMORY_ERROR;
			node->setNodeValue(mapObject);
			getMapObjectsList()->add(node);
			return 0;
		}
	 }
 }
 
MapObject* Map::getMapObject(string id)
{
	for (int i = 0; i < getMapObjectsList()->getSize(); i++) {
		MapObjectNode* node = (MapObjectNode*) getMapObjectsList()->get(i);
 		MapObject* mapObject =  node->getNodeValue();
 		string value = mapObject->getId();
		if (value.compare(id) == 0) {
 			return node->getNodeValue();
 		}
 	}
 	return NULL;
 }

List* Map::getMapObjectsByType(string type) const{
	List * objects = new List();
	for (int i = 0; i < getMapObjectsList()->getSize(); i++) {
		MapObjectNode* node = (MapObjectNode*) getMapObjectsList()->get(i);
 		MapObject* mapObject =  node->getNodeValue();
		string value = mapObject->getType();
		if (value.compare(type) == 0) {
 			MapObjectNode* node = new MapObjectNode();
			node->setNodeValue(mapObject);
			objects->add(node);
		}
 	}
	return objects;
}
